import axios from 'axios'
import store from '@/store'
import { localRead, getToken } from './util'
import iView from 'view-design'
import { Message } from 'iview'

// if (process.env.NODE_ENV === 'development') {
//   axios.defaults.baseURL = "/url";
//   axios.defaults.withCredentials = true;
//   // 编译环境
// } else {
//   axios.defaults.baseURL = "http://localhost:9103/";
//   axios.defaults.withCredentials = true;
// }

// 请求拦截器
axios.interceptors.request.use(
)
// 响应拦截器
axios.interceptors.response.use(
  response => {
    if (response.status === 200) {
      if (response.data.code === 200) {
        Message.success(response.data.msg)
      } else {
        // Message.warning(response.data.msg)
      }
      return Promise.resolve(response.data)
    } else {
      return Promise.reject(response)
    }
  },
  error => {
    if (error.response.status) {
      return Promise.reject(error.response)
    }
  }
)

const addErrorLog = errorInfo => {
  const { statusText, status, request: { responseURL } } = errorInfo
  let info = {
    type: 'ajax',
    code: status,
    mes: statusText,
    url: responseURL
  }
  if (!responseURL.includes('save_error_logger')) store.dispatch('addErrorLog', info)
}

let error = '请求失败'

class HttpRequest {
  constructor (baseUrl = baseURL) {
    this.baseUrl = baseUrl
    this.queue = {}
  }

  getInsideConfig () {
    const config = {
      baseURL: this.baseUrl,
      headers: {
        //
      }
    }
    return config
  }

  destroy (url) {
    delete this.queue[url]
    if (!Object.keys(this.queue).length) {
      // Spin.hide()
    }
  }

  interceptors (instance, url) {
    // 请求拦截
    instance.interceptors.request.use(config => {
      // 添加全局的loading...
      if (!Object.keys(this.queue).length) {
        // Spin.show() // 不建议开启，因为界面不友好
      }
      this.queue[url] = true
      return config
    }, error => {
      return Promise.reject(error)
    })
    // 响应拦截
    instance.interceptors.response.use(res => {
      this.destroy(url)
      const { data, status } = res
      return { data, status }
    }, error => {
      this.destroy(url)
      let errorInfo = error.response
      if (!errorInfo) {
        const { request: { statusText, status }, config } = JSON.parse(JSON.stringify(error))
        errorInfo = {
          statusText,
          status,
          request: { responseURL: config.url }
        }
      }
      addErrorLog(errorInfo)
      return Promise.reject(error)
    })
  }

  request (options) {
    const instance = axios.create()
    options = Object.assign(this.getInsideConfig(), options)
    this.interceptors(instance, options.url)
    return instance(options)
  }
}

export default HttpRequest

export function get (url, params) {
  // params.t = new Date().getTime(); //get方法加一个时间参数,解决ie下可能缓存问题.
  return axios({
    method: 'get',
    url: url,
    params: params,
    headers: {
      Authorization: getToken()
    }
  })
}

export function post (url, data) {
  return axios({
    method: 'post',
    url: url,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      Authorization: getToken()
    },
    data: data
  })
}

export function put (url, data) {
  return axios({
    url: url,
    method: 'put',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      Authorization: getToken()
    },
    data: data
  })
}

export function deletes (url, data) {
  return axios({
    url: url,
    method: 'delete',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
      Authorization: getToken()
    },
    data: data
  })
}
